package interfaz;

import javax.swing.JPanel;

import componentes.Competencia;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;



public class ListarParticipantes extends JPanel {
	

	private JLabel lblListarParticipantes;
	private JLabel lblCompetencia;
	private JScrollPane scrollPane;
	private JButton btnNuevo;
	private JButton btnModificar;
	private JButton btnEliminar;
	private JButton btnVolver;
	private JTextField textCompetencia;
	private JTable tablaParticipantes;
	private String rowData[][] = {};
	private Object columnNames[] = { "Nombre participante o equipo", "Correo" };
	/**
	 * Create the panel.
	 * 
	 * @param panelPrincipal
	 * @param pantallaInicialUserReg
	 *            es el panel donde el usuario registrado puede ver sus tres
	 *            opciones, es el panel que siempre se van a agregar cosas
	 *            //Comentario largo para dsp modificarlo
	 * 
	 */
	public ListarParticipantes(Competencia competencia,	final JPanel panelPrincipal,final PantallaInicialUserReg pantallaInicialUserReg) {
		
		setBackground(new Color(236, 141, 4));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 1.0,Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0,Double.MIN_VALUE };
		setLayout(gridBagLayout);
		
		lblListarParticipantes = new JLabel("Listar Participantes");
		lblListarParticipantes.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblCompetencia = new JLabel("Competencia");
		textCompetencia = new JTextField();
		scrollPane = new JScrollPane();
		btnNuevo = new JButton("  Nuevo   ");
		btnModificar = new JButton("Modificar");
		btnEliminar = new JButton(" Eliminar ");
		btnVolver = new JButton("  Volver  ");
		
	    inicializar(pantallaInicialUserReg, this, panelPrincipal, competencia);
	} //fin constructor
	
	
	private void inicializar(final PantallaInicialUserReg pantallaInicialUserReg, final ListarParticipantes pantallaActual, final JPanel panelPrincipal, final Competencia competencia){
		
		GridBagConstraints gbc_lblListarParticipantes = new GridBagConstraints();
		gbc_lblListarParticipantes.gridwidth = 3;
		gbc_lblListarParticipantes.insets = new Insets(10, 0, 10, 0);
		gbc_lblListarParticipantes.gridx = 0;
		gbc_lblListarParticipantes.gridy = 0;
		add(lblListarParticipantes, gbc_lblListarParticipantes);
		
		GridBagConstraints gbc_lblCompetencia = new GridBagConstraints();
		gbc_lblCompetencia.insets = new Insets(0, 0, 5, 5);
		gbc_lblCompetencia.anchor = GridBagConstraints.EAST;
		gbc_lblCompetencia.gridx = 0;
		gbc_lblCompetencia.gridy = 1;
		add(lblCompetencia, gbc_lblCompetencia);
		
		
		textCompetencia.setEditable(false);
		textCompetencia.setBackground(new Color(236, 141, 4));
		textCompetencia.setBorder(null);
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 1;
		add(textCompetencia, gbc_textField);
		
		
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.insets = new Insets(0, 20, 5, 20);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 2;
		add(scrollPane, gbc_scrollPane);

		tablaParticipantes = new JTable(rowData, columnNames);
		scrollPane.setViewportView(tablaParticipantes);
		
		GridBagConstraints gbc_btnNuevo = new GridBagConstraints();
		gbc_btnNuevo.insets = new Insets(0, 0, 5, 5);
		gbc_btnNuevo.gridx = 0;
		gbc_btnNuevo.gridy = 3;
		add(btnNuevo, gbc_btnNuevo);
		
		GridBagConstraints gbc_btnModificar = new GridBagConstraints();
		gbc_btnModificar.insets = new Insets(0, 0, 5, 5);
		gbc_btnModificar.gridx = 1;
		gbc_btnModificar.gridy = 3;
		add(btnModificar, gbc_btnModificar);
		
		GridBagConstraints gbc_btnEliminar = new GridBagConstraints();
		gbc_btnEliminar.insets = new Insets(0, 0, 5, 0);
		gbc_btnEliminar.gridx = 2;
		gbc_btnEliminar.gridy = 3;
		add(btnEliminar, gbc_btnEliminar);
		
		GridBagConstraints gbc_btnVolver = new GridBagConstraints();
		gbc_btnVolver.insets = new Insets(0, 0, 0, 5);
		gbc_btnVolver.gridx = 1;
		gbc_btnVolver.gridy = 4;
		add(btnVolver, gbc_btnVolver);

				
		/**CODIGO*/
		textCompetencia.setText(competencia.getNombre());
		rowData= new String[competencia.getParticipantes().size()][2];
		for(int i=0; i<competencia.getParticipantes().size();i++){
			rowData[i][0]=competencia.getParticipantes().get(i).getNombre();
			rowData[i][1]=competencia.getParticipantes().get(i).getCorreoElectronico();
		}
		tablaParticipantes = new JTable(rowData, columnNames);
		final DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames){
			public boolean isCellEditable(int row, int column) {
			return false;
			}
			};
	    scrollPane.setViewportView(tablaParticipantes);
	    tablaParticipantes.setModel(tableModel);
	    
	    int row = tablaParticipantes.getSelectedRow();  //ESTA ES LA FILA SELECCIONADA. 
		
		// Listeners botones
		btnNuevo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JPanel AltaParticipante = new AltaParticipante(panelPrincipal, competencia, pantallaInicialUserReg);
				AltaParticipante.setVisible(true);
				setVisible(false);	
				pantallaInicialUserReg.add(AltaParticipante,BorderLayout.CENTER);
			   }
			});
				
		btnModificar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				}
			});		
	
		btnEliminar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				}
			});		
		
		btnVolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				panelPrincipal.setVisible(true);

			}
		});
		
		
		
	}

}
